package com.imyuanma.qingyun.fs.controller;

import com.imyuanma.qingyun.common.factory.BaseDOBuilder;
import com.imyuanma.qingyun.common.model.request.WebRequest;
import com.imyuanma.qingyun.common.model.response.Page;
import com.imyuanma.qingyun.common.model.PageQuery;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.AssertUtil;
import com.imyuanma.qingyun.fs.model.FsFile;
import com.imyuanma.qingyun.fs.service.IFsFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件信息web
 *
 * @author YuanMaKeJi
 * @date 2022-07-24 00:05:08
 */
@RestController
@RequestMapping(value = "/fs/fsFile")
public class FsFileController {

    /**
     * 文件信息服务
     */
    @Autowired
    private IFsFileService fsFileService;

    /**
     * 列表查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/getList")
    public Result<List<FsFile>> getList(@RequestBody WebRequest<FsFile> webRequest) {
        return Result.success(fsFileService.getList(webRequest.getBody()));
    }

    /**
     * 分页查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/getPage")
    public Page<List<FsFile>> getPage(@RequestBody WebRequest<FsFile> webRequest) {
        PageQuery pageQuery = webRequest.buildPageQuery();
        List<FsFile> list = fsFileService.getPage(webRequest.getBody(), pageQuery);
        return Page.success(list, pageQuery);
    }

    /**
     * 统计数量
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/count")
    public Result<Integer> count(@RequestBody WebRequest<FsFile> webRequest) {
        return Result.success(fsFileService.count(webRequest.getBody()));
    }

    /**
     * 查询
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/get")
    public Result<FsFile> get(@RequestBody WebRequest<String> webRequest) {
        AssertUtil.notNull(webRequest.getBody());
        return Result.success(fsFileService.get(webRequest.getBody()));
    }

    /**
     * 新增
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody WebRequest<FsFile> webRequest) {
        FsFile fsFile = webRequest.getBody();
        AssertUtil.notNull(fsFile);
        BaseDOBuilder.fillBaseDOForInsert(fsFile);
        fsFileService.insertSelective(fsFile);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody WebRequest<FsFile> webRequest) {
        FsFile fsFile = webRequest.getBody();
        AssertUtil.notNull(fsFile);
        AssertUtil.notNull(fsFile.getId());
        BaseDOBuilder.fillBaseDOForUpdate(fsFile);
        fsFileService.updateSelective(fsFile);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param webRequest 参数, 待删除主键,多个使用英文逗号拼接
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody WebRequest<String> webRequest) {
        String ids = webRequest.getBody();
        AssertUtil.notBlank(ids);
        if (ids.contains(",")) {
            fsFileService.batchDelete(Arrays.stream(ids.split(",")).collect(Collectors.toList()));
        } else {
            fsFileService.delete(ids);
        }
        return Result.success();
    }

}
